package com.ssyshop.info.activity;

import android.content.pm.ActivityInfo;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.ssyshop.info.R;
import com.ssyshop.info.adapter.MyFragmentPageAdapter;
import com.ssyshop.info.fragment.CartPageFragment;
import com.ssyshop.info.fragment.DdsPageFragment;
import com.ssyshop.info.fragment.HomePageFragment;
import com.ssyshop.info.fragment.SelfPageFragment;
import com.ssyshop.info.view.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RadioGroup main_bottom_ra;
    private RadioButton main_rb_home;
    private HomePageFragment homePageFragment;
    private DdsPageFragment ddsPageFragment;
    private CartPageFragment cartPageFragment;
    private SelfPageFragment selfPageFragment;
    private FragmentManager manager;
    private NoScrollViewPager main_viewpager;
    private List<Fragment> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去除actionbar
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        //隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }


    private void initView() {
        main_bottom_ra= (RadioGroup) findViewById(R.id.main_bottom_ra);
        main_rb_home= (RadioButton) findViewById(R.id.main_rb_home);
        main_viewpager= (NoScrollViewPager) findViewById(R.id.main_viewpager);
        main_viewpager.setNoScroll(true);
        main_viewpager.setOffscreenPageLimit(3);//设置缓存view 的个数（实际有4个，缓存3个+正在显示的1个）
    }
    private void initData() {
        manager=getSupportFragmentManager();
        homePageFragment=new HomePageFragment();
        ddsPageFragment=new DdsPageFragment();
        cartPageFragment=new CartPageFragment();
        selfPageFragment=new SelfPageFragment();
        list=new ArrayList<>();
        list.add(homePageFragment);
        list.add(ddsPageFragment);
        list.add(cartPageFragment);
        list.add(selfPageFragment);
        main_viewpager.setAdapter(new MyFragmentPageAdapter(manager,list));
    }

    private void initEvent() {
        main_bottom_ra.setOnCheckedChangeListener(listener);
        main_rb_home.setChecked(true);
    }

    RadioGroup.OnCheckedChangeListener listener=new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            switch (i){
                case R.id.main_rb_home:
                    main_viewpager.setCurrentItem(0);
//                    manager.beginTransaction().replace(R.id.main_page,homePageFragment).addToBackStack(null).commit();
                    break;
                case R.id.main_rb_dds:
                    main_viewpager.setCurrentItem(1);
//                    manager.beginTransaction().replace(R.id.main_page,ddsPageFragment).addToBackStack(null).commit();
                    break;
                case R.id.main_rb_cart:
                    main_viewpager.setCurrentItem(2);
//                    manager.beginTransaction().replace(R.id.main_page,cartPageFragment).addToBackStack(null).commit();
                    break;
                case R.id.main_rb_self:
                    main_viewpager.setCurrentItem(3);
//                    manager.beginTransaction().replace(R.id.main_page,selfPageFragment).addToBackStack(null).commit();
                    break;
            }
        }
    };

    // 捕获退出按键
    // 退出程序
    private long firstTime = 0;
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) { // 如果两次按键时间间隔大于2秒，则不退出
                    Toast toast = Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
                    toast.show();
                    firstTime = secondTime;// 更新firstTime
                    return true;
                } else {
                    finish();
                    System.exit(0);
                }
            default:
                break;
        }
        return super.onKeyUp(keyCode, event);
    }
}
